package com.an.myphotodemo.image;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Hacky fix for Issue #4 and
 * http://code.google.com/p/android/issues/detail?id=18990
 * 
 * ScaleGestureDetector seems to mess up the touch events, which means that
 * ViewGroups which make use of onInterceptTouchEvent throw a lot of
 * IllegalArgumentException: pointerIndex out of range.
 * 
 * There's not much I can do in my code for now, but we can mask the result by
 * just catching the problem and ignoring it.
 * 
 * 继承于viewpager 可以和photoView一起使用，实现相册图片的左右滑动，放大缩小
 */

public class HackyViewPager extends ViewPager {

	private boolean isLocked;

	public HackyViewPager(Context context) {
		super(context);
		isLocked = false;
	}

	public HackyViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		isLocked = false;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (!isLocked) {
			try {
				return super.onInterceptTouchEvent(ev);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
				return false;
			}
		}
		return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return !isLocked && super.onTouchEvent(event);
	}



//	//单击屏幕结束当前activity
//	@Override
//	public boolean onSingleTapUp(MotionEvent e) {
//		finish();
//		return super.onSingleTapUp(e);
//	}





}
